﻿#include "OperSubsystemBeam.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEASubSysBeam.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"

#include "OperatorsInterface/GraphEventOperator.h"
#include "updateBrowserTable.h"
#include "GUIWidget/AttributeSetDialog.h"

namespace ModelOper
{
    bool OperSubsystemBeam::execGUI()
    {
        return false;
    }
    bool OperSubsystemBeam::execProfession()
    {
        return false;
    }
    void OperSubsystemBeam::editSubsystem(const int seaID)
    {
        if (!_seaMgr)return;
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //编辑对话框
        QObject object;
        object.setObjectName("actionEditBeam");
        Core::FITKActionOperator* oper = Core::FITKOperatorRepo::getInstance()->getOperatorT<Core::FITKActionOperator>("actionEditBeam");
        if (!oper) return;
        oper->setArgs("DataID", seaID);
        oper->setEmitter(&object);
        oper->actionTriggered();
    }
    void OperSubsystemBeam::removeSubsystem(const int seaID)
    {
        //获取管理器
        if (!_seaMgr)return;
        //移除并更新列表
        _seaMgr->removeDataByID(seaID);
        ModelOper::updateBrowserTable();
        EventOper::GraphEventOperator* operGraph = FITKOPERREPO->getOperatorT<EventOper::GraphEventOperator>("GraphPreprocess");
        operGraph->updateSEASubSystems();
    }
    void OperSubsystemBeam::setAttribute(const QString & name, const QString & text, const int attrIndex, const QList<int>& seaID)
    {
        if (!_seaMgr)return;
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsSpectraManager* specMgr = phys->getFITKAcousticsSpectraManager();
        if (specMgr == nullptr) return;
        Acoustics::FITKAcousticsPhysicsPropManager* propMgr = phys->getPhysicsPropManager();
        if (propMgr == nullptr) return;
        //设置对话框
        GUI::AttributeSetDialog dlg(2, mw);
        dlg.setTextLabel(name);
        QList<QString> list;
        if (attrIndex == 1) {
            QList<Acoustics::FITKAcousticsAbsPhysicsProp*> propList = propMgr->getPhysicsProps(Acoustics::FITKAcousticsAbsPhysicsProp::APPBeam);
            for (auto value : propList) {
                list += value->getDataObjectName();
            }
        }
        else if (attrIndex == 2 || attrIndex == 3) {
            QList<Acoustics::FITKAcousticsAbstractSpectra*> specList = specMgr->getSpectraByType(Acoustics::FITKAcousticsAbstractSpectra::Damping);
            for (auto value : specList) {
                list += value->getDataObjectName();
            }
        }
        dlg.setDefaultValueQString(list);
        if (dlg.exec() != QDialog::Accepted) return;

        QString str = dlg.getValueQString();//需要getText
        int ID = 0;
        if (attrIndex == 1)
            ID= propMgr->getDataByName(str)->getDataObjectID();
        else if(attrIndex == 2 || attrIndex == 3)
            ID = specMgr->getDataByName(str)->getDataObjectID();

        for (int id : seaID)
        {
            Acoustics::FITKSEASubSysBeam* beam = dynamic_cast
                <Acoustics::FITKSEASubSysBeam*>(_seaMgr->getDataByID(id));
            if (beam == nullptr) continue;
            //不同列设置不同参数
            switch (attrIndex)
            {
            case 1: //梁物理属性
                beam->setPropSection(ID); break;
            case 2: //x频谱
                beam->setFlexureXSpectraID(ID); break;
            case 3: //y频谱
                beam->setFlexureYSpectraID(ID); break;
            default: break;
            }
        }
        ModelOper::updateBrowserTable();
    }

}